package cn.wx.scholar;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.lang.StringUtils;
import com.csvreader.CsvReader;

import cn.wx.common.utils.JSONUtils;

public class ImgReadExitCsv {

	/**
	 * 111775,http://47.104.244.119:8000//images/head/blockchainImg/ahrash aleshi.jpg
112327,http://47.104.244.119:8000//images/head/dataminingImg/bla? kostanjsek.jpg
	 * @param posturl
	 * @return
	 * @throws IOException
	 */
	
	public static String isImagesTrue(String posturl) throws IOException {        
		URL url = new URL(posturl);
		HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
		urlcon.setRequestMethod("GET");
		//urlcon.setRequestProperty("Content-type","application/x-www-form-urlencoded");        
		if (urlcon.getResponseCode() == HttpURLConnection.HTTP_OK) {            
			System.out.println(HttpURLConnection.HTTP_OK + posturl + ":posted ok!");            
			return "200";        
		} else {            
			System.out.println(urlcon.getResponseCode() + posturl + ":Bad post...");            
			return "404";        
		}
	}

	public static boolean getRource(String source) {          
		try {              
				URL url = new URL(source);            
				URLConnection uc = url.openConnection();            
				InputStream in = uc.getInputStream();            
				if (source.equalsIgnoreCase(uc.getURL().toString()))             
					in.close();              
				return true;          
		} catch (Exception e) {              
			return false;          
		}  
	}


	public static void main(String[] args) {

		final String filePath = "D:\\my_read_csv\\expertImg.csv";
		try {
			
			CsvReader csvReader = new CsvReader(filePath);
			
			File of = new File("D:\\my_read_csv\\export_img_1.csv");
			FileOutputStream ofs = new FileOutputStream(of,true);
			OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
			BufferedWriter output = new BufferedWriter(ofw);

			// 读表头
            csvReader.readHeaders();
            int i = 0;
            while (csvReader.readRecord()){
                // 读一整行
            	String line = csvReader.getRawRecord();
				System.out.println("读取" + (i++) +"行");
				if(StringUtils.isBlank(line)) {
					continue;
				}

				/**
				 * 解析csv
				 */
				String lid = csvReader.get(0);
				String img = csvReader.get(1);
				
				String url = "http://47.104.244.119:8000/" + img;
				
				String urlEncodee= url.replace(" ", "%20");
				
				
				if(!getRource(urlEncodee)) {
					System.out.println("404");
					output.append(lid + ",");
					output.append("http://47.104.244.119:8000/" + img + "\r");
				}
				
				
            }
			
            output.flush();
            output.close();
            output = null;
            
            /**
			 * 关闭读入流
			 */
			csvReader.close();
			csvReader = null;
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
